'''
Created on 2009-11-9

@author: dell
'''
from google.appengine.ext import db;
from datetime import datetime, timedelta

class Member(db.Model):
    '''
    classdocs
    '''
    nickname = db.StringProperty()
    email = db.StringProperty()
    guser = db.UserProperty()
    regdate = db.DateTimeProperty(auto_now_add=True)
    timezone = db.IntegerProperty(default=8) 
    avatar = db.BlobProperty()
    
    def getMemberByEmail(self,email):
        
        query = db.GqlQuery("select * from Member where email = :1",email)
        member = query.get()
        if member:
            return member
        else:
            return False
    def getMemberByNickname(self,nickname):
        query = db.GqlQuery("select * from Member where nickname = :1",nickname)
        member = query.get()
        if member:
            return member
        else:
            return False
    
    def getMember(self,user):
        
        
        query = db.GqlQuery("select * from Member where guser = :1",user)
        member = query.get()
        if member:
            return member
        else:
            member = Member(nickname=user.nickname(),email=user.email(),guser=user)
            member.put()
            return member;
        
    def getNow(self):
        sec = self.timezone*3600
        if sec > 0 :
            timezone = timedelta(seconds=sec)
            now = datetime.now() + timezone
        else:
            sec = 0 - sec
            timezone = timedelta(seconds=sec)
            now = datetime.now() - timezone
        return now    
        